#include "Common.h"
#include "Posix/AppLibPosix_Msg.hpp"

SINT32 main(SINT32 argc, CHAR **args)
{
    CHAR szBuff[1024] = {0}; 
    ULONG ulRet = STAT_SUCCESS;
    APP_LIB_POSIX_MQ_s stPosixMq = {0};
	APP_LIB_POSIX_MQ_MSG_s *pstMsg = (APP_LIB_POSIX_MQ_MSG_s *)szBuff;

	if (argc != 2)
	{
		LOG_ERROR("Usage: ./PosixMq_Srv <Path>");
		return -1;
	}

    APP_LIB_POSIX_MQ_INIT(stPosixMq, args[1], 512, 5);

    ulRet = AppLibPosix_MqOpen(&stPosixMq);
    if (ulRet != STAT_SUCCESS)
    {
        return ulRet;
    }

	while (1)
	{
		memset(szBuff, 0, sizeof(szBuff));
        pstMsg->len = sizeof(szBuff) - sizeof(*pstMsg);
		ulRet = AppLibPosix_MqRecv(&stPosixMq, pstMsg, NULL);
		if (ulRet != STAT_SUCCESS)
		{
			continue;
		}

		LOG_INFO("prio %d, len %d, Data %s", pstMsg->prio, pstMsg->len, (char *)pstMsg->data);
		if (strcmp((char *)pstMsg->data, "quit") == 0)
		{
			break;
		}
	}

    AppLibPosix_MqDestory(&stPosixMq);

    return 0;
}

